package com.guapi.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guapi.mapper.SelfBaseMapper;
import com.guapi.mapper.wrapper.SelfQueryWrapper;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

@Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED)
public class SelfBaseService<T> extends ServiceImpl<SelfBaseMapper<T>, T> {
    @Override
    @Deprecated
    /**
     * 无法使用MP批量保存操作，抛出以下异常：
     * Mapped Statements collection does not contain value for com.guapi.mapper.SelfBaseMapper.insert
     * 推测是由于MP在获取Mapper的时候没有获取父类的相关方法
     */
    public boolean saveBatch(Collection<T> entityList, int batchSize) {
        return super.saveBatch(entityList, batchSize);
    }

    public T getOne(T entity) {
        return baseMapper.select(entity);
    }

    public T getOne(T entity, boolean throwEx) {
        return super.getOne(new SelfQueryWrapper<>(entity), throwEx);
    }

    public List<T> getByConditions(T entity) {
        return baseMapper.selectConditions(entity);
    }

    @Override
    public boolean saveOrUpdate(T entity) {
        return baseMapper.saveOrUpdate(entity);
    }

    public List<T> getByFuzzy(T entity) {
        return baseMapper.selectFuzzy(entity);
    }

    public T deleteEntity(T entity) {
        return baseMapper.deleteEntity(entity);
    }

    public T deleteById(T entity) {
        return baseMapper.deleteById(entity) == 1 ? entity : null;
    }

    public int getCount(T entity) {
        return getByConditions(entity).size();
    }

    public List<T> getByExpired(T entity){
        return baseMapper.getByExpired(entity);
    }
}
